package com.jybd.live.controller.api;

import com.aliyuncs.utils.StringUtils;
import com.jybd.live.entity.ApiResponse;
import com.jybd.live.model.Camera;
import com.jybd.live.model.CameraLiveRel;
import com.jybd.live.model.Record;
import com.jybd.live.service.AliLiveService;
import com.jybd.live.service.CameraLiveRelService;
import com.jybd.live.service.CameraService;
import com.jybd.live.service.RecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/camera")
public class CameraController {

    private CameraService cameraService;
    private CameraLiveRelService cameraLiveRelService;
    private RecordService recordService;
    private AliLiveService aliLiveService;

    //log4j2日志
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public CameraController(CameraService cameraService, CameraLiveRelService cameraLiveRelService, RecordService recordService, AliLiveService aliLiveService) {
        this.cameraService = cameraService;
        this.cameraLiveRelService = cameraLiveRelService;
        this.recordService = recordService;
        this.aliLiveService = aliLiveService;
    }



    /**
     * 获取所有摄像头
     * @return
     */
    @RequestMapping("getAll")
    public ApiResponse liveList() {
        ApiResponse apiResponse = new ApiResponse();
        try {
            List<Camera> list = cameraService.getAll();
            apiResponse.setStatus(ApiResponse.STATUS.OK);
            apiResponse.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            apiResponse.setStatus(ApiResponse.STATUS.ERROR);
        }
        return apiResponse;
    }

    /**
     * 获取回放列表
     * @return
     */
    @RequestMapping("recList")
    public ApiResponse getRecList(@RequestParam(value = "cameraId") Integer cameraId){
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setStatus(ApiResponse.STATUS.OK);
        CameraLiveRel relInUse = cameraLiveRelService.getCameraLiveRelInUse(cameraId);
        List<Record> list;
        if(null != relInUse){
             list = recordService.getRecord(relInUse.getApp(),relInUse.getStream());
        }else {
            list = new ArrayList<>();
        }
        apiResponse.setData(list);
        return apiResponse;
    }

    /**
     * 获取回放地址
     * @return
     */
    @RequestMapping("recURI")
    public ApiResponse getRecURI(Integer recordId){
        ApiResponse apiResponse = new ApiResponse();
        Record record = recordService.getById(recordId);
        if(null != record && !StringUtils.isEmpty(record.getUri())){
            String uri = aliLiveService.getLiveRecordURL(record.getUri());
            if(!StringUtils.isEmpty(uri)){
                apiResponse.setStatus(ApiResponse.STATUS.OK);
                Map map = new HashMap();
                map.put("uri",uri);
                apiResponse.setData(map);
            }else {
                apiResponse.setStatus(ApiResponse.STATUS.PARAMS_ERROR);
                log.error("not find record uri for recordId:" + record);
            }
        }else {
            apiResponse.setStatus(ApiResponse.STATUS.PARAMS_ERROR);
        }
        return apiResponse;
    }
}
